#include "formdevinfo.h"
#include "ui_formdevinfo.h"

#include <QCloseEvent>


QString FormDevInfo::gas_name = "四氢噻吩";
QString FormDevInfo::gas_unit = "mg/m³";

FormDevInfo::FormDevInfo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormDevInfo)
{
    ui->setupUi(this);

    ui->lineEdit->setText(common_data::init_config_param.deviceId);
    ui->comboBox_1->setCurrentIndex(common_data::init_config_param.gas_name_index);
    ui->comboBox_2->setCurrentIndex(common_data::init_config_param.gas_unit_index);
    ui->comboBox_3->setCurrentIndex(common_data::init_config_param.power_supply_index);
    ui->comboBox_4->setCurrentIndex(common_data::init_config_param.baud_index);

    ui->label_11->setText(common_data::init_config_param.dev_software_version);
    ui->label_18->setText(common_data::init_config_param.dev_hardware_version);

    ui->lineEdit->installEventFilter(this); //给控件安装事件过滤器

    // 连接返回按钮
    connect(ui->backButton, &QPushButton::clicked, this, [this]() {
        emit backRequested();
        this->hide();
    });

    if(common_data::init_config_param.gas_name_index == 0)
    {
        gas_name = "四氢噻吩";
    }
    else
    {
        gas_name = "甲烷";
    }

    this->move(0, 50);
    this->show();
}

FormDevInfo::~FormDevInfo()
{
    if(keyboard)
    {
        keyboard->deleteLater();
    }

    delete ui;
}

void FormDevInfo::delay_ms(unsigned int ms)
{
    QTime Time_set = QTime::currentTime().addMSecs(ms);
    while( QTime::currentTime() < Time_set )
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}


/**************************************************************************************
函数名称：eventFilter
功   能：按键点击触发事件函数
输   入：*obj  *e
输   出：无
返   回：无
***************************************************************************************/
// 在 eventFilter 中增加类型检查
bool FormDevInfo::eventFilter(QObject *obj, QEvent *e)
{
    if (e->type() == QEvent::MouseButtonPress)
    {
        QMouseEvent *event = static_cast<QMouseEvent*>(e);
        if (event->button() == Qt::LeftButton)
        {
            // 👇 类型安全转换，避免非 QLineEdit 触发
            if (QLineEdit* edit = qobject_cast<QLineEdit*>(obj))
            {
                if (keyboard)
                {
                    keyboard->setInputLineEdit(edit);
                }
                else
                {
                    keyboard = new FormKeyBoard(this, edit);
                    connect(keyboard, &FormKeyBoard::formkeyboard_close_signal,
                            this, &FormDevInfo::formkeyboard_close_slot);
                }
            }
        }
    }
    return false;
}

/**************************************************************************************
函数名称：formkeyboard_close_slot
功   能：键盘关闭触发事件函数
输   入：无
输   出：无
返   回：无
***************************************************************************************/
 void FormDevInfo::formkeyboard_close_slot()
 {
     if(keyboard)
     {
         keyboard->deleteLater();
         keyboard = nullptr;
     }
 }

//确认保存
void FormDevInfo::on_pushButton_clicked()
{
    if( ui->comboBox_1->currentIndex() == 0)
    {
        gas_name = "四氢噻吩";
    }
    else
    {
        gas_name = "甲烷";
    }

    common_data::init_config_param.deviceId = ui->lineEdit->text();

    // 以键值对的形式写入数据

    common_data::init_config_param.deviceId = ui->lineEdit->text();
    common_data::init_config_param.gas_name_index = ui->comboBox_1->currentIndex();
    common_data::init_config_param.gas_unit_index = ui->comboBox_2->currentIndex();
    common_data::init_config_param.power_supply_index = ui->comboBox_3->currentIndex();
    common_data::init_config_param.baud_index = ui->comboBox_4->currentIndex();

    QSettings * settings = new QSettings(CFG_FILE_PATH, QSettings::IniFormat);

    settings->setValue("Project/deviceId", ui->lineEdit->text());
    settings->setValue("Project/gas_name_index", ui->comboBox_1->currentIndex());
    settings->setValue("Project/gas_unit_index", ui->comboBox_2->currentIndex());
    settings->setValue("Project/power_supply_index", ui->comboBox_3->currentIndex());
    settings->setValue("Project/baud_index", ui->comboBox_4->currentIndex());

    delete settings;

    ui->pushButton->setStyleSheet("color: rgb(255, 255, 255);background-color: rgb(32, 74, 135)");
    delay_ms(100);
    ui->pushButton->setStyleSheet("color: rgb(255, 255, 255);");
}

//读取设备信息
void FormDevInfo::on_pushButton_2_clicked()
{

    ui->pushButton_2->setStyleSheet("color: rgb(255, 255, 255);background-color: rgb(32, 74, 135)");
    delay_ms(100);
    ui->pushButton_2->setStyleSheet("color: rgb(255, 255, 255);");

    ui->lineEdit->setText(common_data::init_config_param.deviceId);
    ui->label_11->setText(common_data::init_config_param.dev_software_version);
    ui->label_18->setText(common_data::init_config_param.dev_hardware_version);
}

void FormDevInfo::ttyusb2_to_devinfo_slot(QString str, QString str_1)
{
    ui->label_14->setText(str);
    ui->label_15->setText(str_1);
    common_data::ttyUSB2_to_devinfo_flag = true;
}

void FormDevInfo::rs232_to_devinfo_slot(QString deviceId)
{
    ui->lineEdit->setText(deviceId);
}

void FormDevInfo::closeEvent(QCloseEvent *event)
{
    event->ignore();
    emit backRequested();
    this->hide();
}
